#!/bin/bash
# 此脚本主要用于数据库备份
# 1.自动抓取所有数据库【排除个别】进行备份
# 2.自动对数据库进行分库分表备份
# 3.对备份后的数据库，以库为单位进行压缩加密

DB_NAME=`mysql -uadmin -pzjht0724# -e "show databases;"|sed '1,2d'`
mkdir  /home/zj-db/backup/$(date +%F) -p
cd /home/zj-db/backup/$(date +%F)
# backup every database with every table
for db in $DB_NAME
do
	if [[ "$db" != 'information_schema' && "$db" != 'performance_schema' && "$db" != 'mysql' && "$db" != 'sys' ]];then
		mkdir /home/zj-db/backup/$(date +%F)/$db -p
        	TB_NAME=`mysql -uadmin -pzjht0724# -e "show tables from $db;"|sed '1d'`
        	for tb in $TB_NAME
        	do
        	        mysqldump -uadmin -pzjht0724# --lock-tables=0 $db $tb > /home/zj-db/backup/$(date +%F)/${db}/${tb}.sql
        	done
		zip -r -Pzjht1234 ${db}.zip $db
		rm -rf $db
	fi
done

